<?php

namespace PhalconTutorial\Models;

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Uniqueness;

class Users extends Model
{
	/**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $name;

    /**
     *
     * @var string
     */
    public $email;

    /**
     *
     * @var string
     */
    public $password;

    /**
     *
     * @var string
     */
    public $mustChangePassword;

    /**
     *
     * @var string
     */
    public $profilesId;

    /**
     *
     * @var string
     */
    public $banned;

    /**
     *
     * @var string
     */
    public $suspended;

    /**
     *
     * @var string
     */
    public $active;


    /*
    * Before create user assign a password
    */
    public function beforeValidationOnCreate()
    {
    	if(empty($this->password))
    	{
    		// Generate a plain temporary password
            $tempPassword = preg_replace('/[^a-zA-Z0-9]/', '', base64_encode(openssl_random_pseudo_bytes(12)));

            // The user must change its password in first login
            $this->mustChangePassword = 'Y';

            // Use this password as default
            $this->password = $this->getDI()
                ->getSecurity()
                ->hash($tempPassword);
    	}
    	else
    	{
    		// The user must not change its password in fisrt login
    		$this->mustChangePassword = 'N';
    	}

    	// The account must be confirmed 
    	$this->active = 'N';

    	// The account is not suspended by default
        $this->suspended = 'N';

        // The account is not banned by default
        $this->banned = 'N';

    }

    public function afterSave()
    {
    	if($this->active == 'N')
    	{
    		$emailConfirmation = new EmailConfirmations();
    		$emailConfirmation->userId = $this->id;
    		if($emailConfirmation->save())
    		{
    			$this->getDI()
                    ->getFlash()
                    ->notice('A confirmation mail has been sent to ' . $this->email);
    		}
    	}
    }

    public function validation()
    {
    	$this->validate(new Uniqueness(array(
    		"field" => "email",
            "message" => "The email is already registered"
    	)));
    }

    public function initialize()
    {
    	$this->belongsTo('profilesId', 'PhalconTutorial\Models\Profiles', 'id', array(
            'alias' => 'profile',
            'reusable' => true
        ));
        
        
    	$this->hasMany('id' , 'PhalconTutorial\Models\SuccessLogins' , 'userId' , array('alias' => 'successLogins' , 'foreignKey' => array('message' => 'User cannot be deleted because he/she has activity in the system')));

    	$this->hasMany('id', 'PhalconTutorial\Models\PasswordChanges', 'usersId', array(
            'alias' => 'passwordChanges',
            'foreignKey' => array(
                'message' => 'User cannot be deleted because he/she has activity in the system'
            )
        ));

        $this->hasMany('id', 'PhalconTutorial\Models\ResetPasswords', 'usersId', array(
            'alias' => 'resetPasswords',
            'foreignKey' => array(
                'message' => 'User cannot be deleted because he/she has activity in the system'
            )
        ));

    }
}